package com.example.demo.service;

import com.example.demo.main.User;
import com.example.demo.main.UserOrder;
import com.example.demo.main.UserOrderExample;
import com.example.demo.mapper.UserOrderMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class UseSQLOfOrder {

    private static final String ORDER_STATUS = "未支付";

    @Autowired
    private UserOrderMapper userOrderMapper;
    @Autowired
    private UserOrderExample userOrderExample;

    public Boolean selectByRemoveDateLessAndDelete(Date date) {
        userOrderExample.clear();
        userOrderExample.or().andRemoveTimeLessThanOrEqualTo(date).andOrderStatusEqualTo(ORDER_STATUS);
        List<UserOrder> orderList = userOrderMapper.selectByExample(userOrderExample);
        if (orderList.size() < 1) return true;
        boolean flag = true;
        for (UserOrder userOrder : orderList
        ) {
            if (userOrderMapper.deleteByPrimaryKey(userOrder.getOrderId()) < 1)
                flag = false;
        }
        return flag;
    }

    public Integer insertByClass(UserOrder userOrder) {
        return userOrderMapper.insertSelective(userOrder);
    }

    public List<UserOrder> selectByNameAndNoPay(String name) {
        userOrderExample.clear();
        userOrderExample.or().andUserNameEqualTo(name).andOrderStatusEqualTo(ORDER_STATUS);
        return userOrderMapper.selectByExample(userOrderExample);
    }

    public Integer deleteByID(String orderID) {
        return userOrderMapper.deleteByPrimaryKey(orderID);
    }

    public List<UserOrder> selectAllOfLimit(Integer index, Integer offset) {
        return userOrderMapper.selectAllOfLimit(ORDER_STATUS, index, offset);
    }

    public Integer selectAllCountOfSta() {
        return userOrderMapper.selectAllCountOfOrdersat(ORDER_STATUS);
    }

    public Integer updateNoPay(UserOrder order) {
        userOrderExample.clear();
        userOrderExample.or().andOrderStatusEqualTo(ORDER_STATUS);
        UserOrder userOrder = new UserOrder();
        userOrder.setOrderStatus("等待商家");
        userOrder.setPersion(order.getPersion());
        userOrder.setAddress(order.getAddress());
        userOrder.setPhone(order.getPhone());
        return userOrderMapper.updateByExampleSelective(userOrder, userOrderExample);
    }

    public List<UserOrder> selectNoPay() {
        userOrderExample.clear();
        userOrderExample.or().andOrderStatusEqualTo(ORDER_STATUS);
        return userOrderMapper.selectByExample(userOrderExample);
    }

    public Integer selectAllCount() {
        return userOrderMapper.selectAllCount();
    }

    public List<UserOrder> selectOfPay() {
        return userOrderMapper.selectOrderOfPay(ORDER_STATUS);
    }

    public List<UserOrder> selectByStoreName(String storeName) {
        userOrderExample.clear();
        userOrderExample.or().andStoreNameEqualTo(storeName).andOrderStatusEqualTo("等待商家");
        return userOrderMapper.selectByExample(userOrderExample);
    }

    public Integer updateStoreorder(String orderId) {
        UserOrder userOrder = new UserOrder();
        String s = "交易中";
        userOrder.setOrderId(orderId);
        userOrder.setOrderStatus(s);
        return userOrderMapper.updateByPrimaryKeySelective(userOrder);
    }

}
